﻿//----------------------------------------------------------------------------------------------------
// A NodeInstance is the representation of a node in a level
//----------------------------------------------------------------------------------------------------

package HG.Visual
{
	import flash.display.*;
	import flash.events.*;
	import flash.utils.Timer;
	
	import fl.transitions.Tween;
	import fl.transitions.easing.*;
	import fl.transitions.TweenEvent;
	
	public class VisualObject extends MovieClip
	{
		var m_animTimer:Timer;
		var m_setVisible:Boolean;
		var m_open:Boolean;
		
		var m_tween:Tween;
		
		//----------------------------------------------------------------------------------------------------
		public function VisualObject()
		{
			m_animTimer = new Timer(0);
			m_animTimer.addEventListener(TimerEvent.TIMER, SetVisibleDelayInternal);
			m_open = false;
			
			this.alpha = 0;
			this.visible = false;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function SetVisibleDelay(vis:Boolean, delay:Number)
		{
			m_setVisible = vis;
			m_animTimer.reset();
			m_animTimer.delay = delay;
			m_animTimer.start();
		}
		
		//----------------------------------------------------------------------------------------------------
		public function SetVisibleDelayInternal(evt:TimerEvent)
		{
			m_animTimer.stop();
			SetVisible(m_setVisible);
		}
		
		//----------------------------------------------------------------------------------------------------
		public function SetVisible(vis:Boolean)
		{
			if (vis == m_open)
				return;
				
			m_open = vis;
			
			if (m_open)
			{
				//this.gotoAndPlay('in');
				this.visible = true;
				m_tween = new Tween(this, "alpha", Strong.easeInOut, this.alpha, 1, 1, true);
			}
			else
			{
				m_tween = new Tween(this, "alpha", Strong.easeInOut, this.alpha, 0, 1, true);
				m_tween.addEventListener(TweenEvent.MOTION_FINISH, onFinish);
			}
		}
		
		//----------------------------------------------------------------------------------------------------
		function onFinish(e:TweenEvent) 
		{
			this.visible = false;
		}
		
		//----------------------------------------------------------------------------------------------------
	}
	
}